package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC605 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public boolean canPlaceFlowers(int[] flowerbed, int n) {
            int firstOnePos = -2, secondOnePos = 0, curZeroCount = 0, resCount = 0;
            while (secondOnePos < flowerbed.length) {
                if (flowerbed[secondOnePos++] == 0) continue;

                curZeroCount = secondOnePos - firstOnePos - 2;
                resCount += (curZeroCount - 1) / 2;
                firstOnePos = secondOnePos - 1;
                if (resCount >= n) return true;
            }

            if (flowerbed[secondOnePos - 1] == 0) {
                //末尾为0情况
                curZeroCount = secondOnePos - firstOnePos - 1;
                resCount += curZeroCount / 2;
                if (resCount >= n) return true;
            }

            return false;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[1,0,0,0,0]");
        System.out.println(solution.canPlaceFlowers(arr, 2));
    }

    public static void main(String[] args) throws Exception {
        LC605 an = new LC605();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
